package org.jeecg.modules.hbjh.convert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.common.util.Base64Util;
import org.jeecg.modules.hbjh.jsvo.*;
import org.jeecg.modules.hbjh.jsvo.skfpkj.JsXxSkfpkj;
import org.jeecg.modules.hbjh.jsvo.skfpkj.JsXxSkfpkjmx;
import org.jeecg.modules.api.xxApi.service.IDjts;
import org.jeecg.modules.api.xxApi.vo.*;
import org.jeecg.modules.xxgx.common.enums.TdyslxDmEnum;
import org.jeecg.modules.xxgx.common.util.FPUtil;
import org.jeecg.modules.xxgx.common.util.InvoiceConvertUtils;
import org.jeecg.modules.xxgx.common.vo.HsBhsConvertVo;
import org.jeecg.modules.xxgx.common.vo.SplitDzdhVo;
import org.jeecg.modules.xxgx.common.vo.SplitYhkhVo;
import org.jeecg.modules.xxgx.djgl.entity.cezs.Cezs;
import org.jeecg.modules.xxgx.djgl.entity.tdyw.*;
import org.jeecg.modules.xxgx.kpzdgl.entity.CsXxKpzd;
import org.jeecg.modules.xxgx.spgl.entity.CsPbCode;
import org.jeecg.modules.xxgx.spgl.service.ICsPbCodeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 将河北交行推送的金穗的接口数据转换位共享的接口调用数据
 */
@Slf4j
@Component
public class TaxRequestParamConvert {

    private static TaxRequestParamConvert convert;

    @Autowired
    private ICsPbCodeService pbCodeService;

    @Autowired
    private IDjts iDjts;

    /**
     * 加载容器中资源
     * 项目启动生成调用
     */
    @PostConstruct
    private void init() {
        convert = this;
    }

    /**
     * 单据推送数据转换
     */
    public static Result<JSONObject> djtsParamConvert(JSONObject convertData) {
        String error = null;
        List<XxApiDjtsParam> list = new ArrayList<>();
        try {
            JSONArray dataList = convertData.getJSONArray("data");
            for (Object data : dataList) {
                JSONObject jsonData = JSON.parseObject(JSON.toJSONString(data));
                //金税单据
                JsXxDjtsDj jsdj = jsonData.toJavaObject(JsXxDjtsDj.class);
                XxApiDjtsParam taxdj = new XxApiDjtsParam();
                BeanUtils.copyProperties(jsdj, taxdj);
                taxdj.setYwdjbh(jsdj.getDjbh());
                taxdj.setZsfs("0");
                taxdj.setGfzrrbs(jsdj.getGmfZrrbs());
                taxdj.setHcyy(jsdj.getChyydm());
                //转换客户地址电话
                SplitDzdhVo dzdhVo = InvoiceConvertUtils.splitDzdh(jsdj.getKhdzdh());
                taxdj.setKhdh(dzdhVo.getDh());
                taxdj.setKhdz(dzdhVo.getDz());
                //转换银行卡号
                SplitYhkhVo yhkhVo = InvoiceConvertUtils.splitYhkh(jsdj.getKhyhzh());
                taxdj.setKhyh(yhkhVo.getYhmc());
                taxdj.setKhzh(yhkhVo.getYhkh());
                taxdj.setKhlxfs(jsdj.getGmfMobile());
                taxdj.setKhlxyx(jsdj.getGmfEmail1());
                taxdj.setSfzsgmfyhzh("");
                taxdj.setSfzsxsfyhzh("");
                JsTdyhVo jsTdyhVo = jsdj.getTdhy();
                String tdyslx = "";
                String tdysnr = "";
                if(jsTdyhVo != null){
                    taxdj.setTdyslxdm(jsdj.getTdhy().getTdyslx());
                    tdyslx = jsTdyhVo.getTdyslx();
                    tdysnr = jsTdyhVo.getTdysnr();
                }
                if (StringUtils.isNotEmpty(jsdj.getYqdfphm())) {
                    taxdj.setYfphm(jsdj.getYqdfphm());
                }
                taxdj.setYjdz("");
                taxdj.setYjjsr("");
                //转换商品明细
                List<XxApiDjtsMxParam> items = jsdj.getMxxx().parallelStream().map(jsmx -> {
                    XxApiDjtsMxParam taxmx = new XxApiDjtsMxParam();
                    BeanUtils.copyProperties(jsmx, taxmx);
                    taxmx.setMxhh(Integer.valueOf(jsmx.getDjhh()));
                    String spbm = jsmx.getSpbm();
                    CsPbCode code = convert.pbCodeService.getById(spbm);
                    if (code != null) {
                        taxmx.setSsmc(code.getShortName());
                    }
                    HsBhsConvertVo hsBhsConvertVo = new HsBhsConvertVo();
                    BeanUtils.copyProperties(taxmx, hsBhsConvertVo);
                    FPUtil.hsBhsConvert(hsBhsConvertVo);
                    taxmx.setTaxje(hsBhsConvertVo.getTaxje());
                    BeanUtils.copyProperties(hsBhsConvertVo, taxmx);
                    return taxmx;
                }).collect(Collectors.toList());
                taxdj.setItems(items);
                if (StringUtils.isNotEmpty(tdyslx) && StringUtils.isNotEmpty(tdysnr)) {
                    if (tdyslx.equals(TdyslxDmEnum.HWYSFW)) { //货物运输服务
                        JSONArray hwysfwArray = JSON.parseArray(tdysnr);
                        List<Hwysfwmx> hwysfw = hwysfwArray.parallelStream().map(jshwys -> {
                            JSONObject jshwysData = JSON.parseObject(JSON.toJSONString(jshwys));
                            Hwysfwmx hwysfwmx = jshwysData.toJavaObject(Hwysfwmx.class);
                            hwysfwmx.setYshwmc(jshwysData.containsKey("yshwmc1") ? jshwysData.getString("yshwmc1") : "");
                            return hwysfwmx;
                        }).collect(Collectors.toList());
                        taxdj.setHwysfw(hwysfw);
                    } else if (tdyslx.equals(TdyslxDmEnum.JZFW)) {//建筑服务
                        JSONObject jzfwJson = JSONObject.parseObject(tdysnr);
                        Jzfw jzfw = JSONObject.parseObject(tdysnr, Jzfw.class);
                        jzfw.setJzfwfsdxxdz(jzfwJson.containsKey("fullAddress") ? jzfwJson.getString("fullAddress") : "");
                        taxdj.setJzfw(jzfw);
                    } else if (tdyslx.equals(TdyslxDmEnum.BDCXS)) {//不动产销售服务
                        JSONObject bdcxsfwJson = JSONObject.parseObject(tdysnr);
                        Bdcxsfw bdcxsfw = JSONObject.parseObject(tdysnr, Bdcxsfw.class);
                        bdcxsfw.setWqhtbah(bdcxsfwJson.containsKey("wqhtbabh") ? bdcxsfwJson.getString("wqhtbabh") : "");
                        bdcxsfw.setBdcdzxxdz(bdcxsfwJson.containsKey("fulladdress") ? bdcxsfwJson.getString("fulladdress") : "");
                        taxdj.setBdcxsfw(bdcxsfw);
                    } else if (tdyslx.equals(TdyslxDmEnum.BDCJYZLFW)) {//不动产经营租赁服务
                        JSONObject bdcjyzlfwJson = JSONObject.parseObject(tdysnr);
                        Bdcjyzlfw bdcjyzlfw = JSONObject.parseObject(tdysnr, Bdcjyzlfw.class);
                        bdcjyzlfw.setBdcdzxxdz(bdcjyzlfwJson.containsKey("fulladdress") ? bdcjyzlfwJson.getString("fulladdress") : "");
                        String zlqqz = bdcjyzlfwJson.containsKey("zlqqz") ? bdcjyzlfwJson.getString("zlqqz") : "";
                        if (StringUtils.isNotEmpty(zlqqz)) {
                            try {
                                String[] zlrqqstr = zlqqz.split(" ");
                                if (zlrqqstr != null) {
                                    bdcjyzlfw.setZlrqq(DateUtils.parseDate(zlrqqstr[0], DateUtils.YYYY_MM_DD));
                                }
                                if (zlrqqstr != null) {
                                    bdcjyzlfw.setZlrqz(DateUtils.parseDate(zlrqqstr[1], DateUtils.YYYY_MM_DD));
                                }
                            } catch (Exception e) {
                                log.error("不动产经营租赁服务租赁起止日期格式有误;");
                                error += "不动产经营租赁服务租赁起止日期格式有误;";
                            }
                        }
                        taxdj.setBdcjyzlfw(bdcjyzlfw);

                    } else if (tdyslx.equals(TdyslxDmEnum.LKYSFW)) {//旅客运输服务
                        JSONArray lkysfwmxArray = JSON.parseArray(tdysnr);
                        List<Lkysfwmx> lkysfwmxs = lkysfwmxArray.parallelStream().map(jslkysfwmx -> {
                            JSONObject lkysfwmxData = JSON.parseObject(JSON.toJSONString(jslkysfwmx));
                            Lkysfwmx lkysfwmx = lkysfwmxData.toJavaObject(Lkysfwmx.class);
                            //
                            lkysfwmx.setSfzjlx(lkysfwmxData.containsKey("cxrzjlx") ? lkysfwmxData.getString("cxrzjlx") : "");

                            return lkysfwmx;
                        }).collect(Collectors.toList());
                        taxdj.setLkysfwmx(lkysfwmxs);
                    }
                }
                list.add(taxdj);
            }
        } catch (Exception e) {
            e.printStackTrace();
            error += e.getMessage();
        }
        if (StringUtils.isNotEmpty(error)) {
            return Result.error(error);
        } else {
            XxApiDjtsListParam param = new XxApiDjtsListParam();
            if(list.size() > 1){
                //异步调用
                param.setAsync("1");
                param.setCallBackUrl("http://localhost:443/taxshare/convert/response/djtsCallback");
                param.setList(list);
            }else{
                //同步调用
                param.setAsync("0");
                param.setList(list);
            }
            return Result.OK("单据转换成功", JSONObject.parseObject(JSONObject.toJSONString(param)));
        }
    }

    /**
     * 发票开具转换
     *
     * @param convertData
     * @return
     */
    public static Result fpkjParamConvert(JSONObject convertData) {
        JsXxFpkj fpkj = convertData.toJavaObject(JsXxFpkj.class);
        List<JsXxFpkjMx> jsmxxx = fpkj.getMxxx();
        List<JsXxHwysfw> jshwysfwmx = fpkj.getHwysfwmx();
        JsXxJzfw jsjzfw = fpkj.getJzfw();
        JsXxbdcxsfw jsbdcxsfw = fpkj.getBdcxsfw();
        JsXxbdcjyzlfw jsbdcjyzlfw = fpkj.getBdcjyzlfw();
        List<JsXxLkysfw> jslkysfwmx = fpkj.getLkysfwmx();
        List<JsXxfjys> jsfjysmx = fpkj.getFjysmx();
        List<JsXxCepz> jscepzmxList = fpkj.getCepzmxList();

        CsXxKpzd csXxKpzd = convert.iDjts.queryKpzdBykpzddm(fpkj.getKpzddm());
        XxApiFpkjParam xxApiFpkjParam = new XxApiFpkjParam();
        BeanUtils.copyProperties(fpkj, xxApiFpkjParam);
        xxApiFpkjParam.setFpsqlsh(fpkj.getDjbh());
        xxApiFpkjParam.setSjlx("jkkp");
        xxApiFpkjParam.setSjly("sglr");
        xxApiFpkjParam.setZzfpbz("0");
        if (StringUtils.isNotBlank(fpkj.getKhdzdh()) && fpkj.getKhdzdh().contains(",")) {
            String[] khdzdh = fpkj.getKhdzdh().split(",");
            xxApiFpkjParam.setKhdz(khdzdh[0]);
            xxApiFpkjParam.setKhdh(khdzdh[1]);
        }
        if (StringUtils.isNotBlank(fpkj.getKhyhzh()) && fpkj.getKhyhzh().contains(",")) {
            String[] khyhzh = fpkj.getKhyhzh().split(",");
            xxApiFpkjParam.setKhyh(khyhzh[0]);
            xxApiFpkjParam.setKhzh(khyhzh[1]);
        }
        xxApiFpkjParam.setKhlxyx(fpkj.getGmfEmail());
        xxApiFpkjParam.setKhlxfs(fpkj.getGmfMobile());
        xxApiFpkjParam.setTspz("00");
        xxApiFpkjParam.setXfmc(csXxKpzd.getNsrmc());
        xxApiFpkjParam.setXfsh(csXxKpzd.getNsrsbh());
        //处理销方地址和电话
        if (StringUtils.isNotBlank(fpkj.getXfdzdh()) && fpkj.getXfdzdh().contains(",")) {
            String[] xfdzdh = fpkj.getXfdzdh().split(",");
            xxApiFpkjParam.setXfdz(xfdzdh[0]);
            xxApiFpkjParam.setXfdh(xfdzdh[1]);
        }
        if (StringUtils.isBlank(xxApiFpkjParam.getXfdz())) {
            xxApiFpkjParam.setXfdz(csXxKpzd.getXfdz());
        }
        if (StringUtils.isBlank(xxApiFpkjParam.getXfdh())) {
            xxApiFpkjParam.setXfdh(csXxKpzd.getXfdh());
        }
        //处理销方银行和银行账号
        if (StringUtils.isNotBlank(fpkj.getXfyhzh()) && fpkj.getXfyhzh().contains(",")) {
            String[] xfyhzh = fpkj.getXfyhzh().split(",");
            xxApiFpkjParam.setXfyh(xfyhzh[0]);
            xxApiFpkjParam.setXfzh(xfyhzh[1]);
        }
        if (StringUtils.isBlank(xxApiFpkjParam.getXfyh())) {
            xxApiFpkjParam.setXfyh(csXxKpzd.getXfyh());
        }
        if (StringUtils.isBlank(xxApiFpkjParam.getXfzh())) {
            xxApiFpkjParam.setXfzh(csXxKpzd.getXfyhzh());
        }
        //处理开票人 复核人 收款人
        if (StringUtils.isBlank(xxApiFpkjParam.getKpr())) {
            xxApiFpkjParam.setKpr(csXxKpzd.getKpr());
        }
        if (StringUtils.isBlank(xxApiFpkjParam.getFhr())) {
            xxApiFpkjParam.setFhr(csXxKpzd.getFhr());
        }
        if (StringUtils.isBlank(xxApiFpkjParam.getSkr())) {
            xxApiFpkjParam.setSkr(csXxKpzd.getSkr());
        }
        if (jsmxxx.size() > 8) {
            xxApiFpkjParam.setQdbz("1");
        } else {
            xxApiFpkjParam.setQdbz("0");
        }
        xxApiFpkjParam.setZzfpbz(fpkj.getZppzdm());
        xxApiFpkjParam.setGfzrrbs(fpkj.getGmfzrrbs());
        xxApiFpkjParam.setHcyy(fpkj.getKjly());

        //处理明细
        List<XxApiFpkjMxParam> xxApiFpkjMxParamList = new ArrayList<>();
        for (JsXxFpkjMx jsXxFpkjMx : jsmxxx) {
            XxApiFpkjMxParam xxApiFpkjMxParam = new XxApiFpkjMxParam();
            BeanUtils.copyProperties(jsXxFpkjMx, xxApiFpkjMxParam);
            xxApiFpkjMxParam.setMxhh(Integer.valueOf(jsXxFpkjMx.getDjhh()));
            String spbm = jsXxFpkjMx.getSsbm();
            CsPbCode code = convert.pbCodeService.getById(spbm);
            if (code != null) {
                xxApiFpkjMxParam.setSpbm(spbm);
                xxApiFpkjMxParam.setSsmc(code.getShortName());
            }
            HsBhsConvertVo hsBhsConvertVo = new HsBhsConvertVo();
            BeanUtils.copyProperties(xxApiFpkjMxParam, hsBhsConvertVo);
            FPUtil.hsBhsConvert(hsBhsConvertVo);
            xxApiFpkjMxParam.setTaxje(hsBhsConvertVo.getTaxje());
            BeanUtils.copyProperties(hsBhsConvertVo, xxApiFpkjMxParam);
            xxApiFpkjMxParamList.add(xxApiFpkjMxParam);
        }
        xxApiFpkjParam.setFpmxs(xxApiFpkjMxParamList);

        //处理货物运输服务
        if (jshwysfwmx != null && !jshwysfwmx.isEmpty()) {
            List<Hwysfwmx> hwysfwmxList = new ArrayList<>();
            for (JsXxHwysfw jsXxHwysfw : jshwysfwmx) {
                Hwysfwmx hwysfwmx = new Hwysfwmx();
                BeanUtils.copyProperties(jsXxHwysfw, hwysfwmx);
                hwysfwmx.setYshwmc(jsXxHwysfw.getYshwmc1());
                hwysfwmxList.add(hwysfwmx);
            }
            xxApiFpkjParam.setTdywHwysfwList(hwysfwmxList);
        }

        //处理建筑服务
        if (jsjzfw != null) {
            Jzfw jzfw = new Jzfw();
            BeanUtils.copyProperties(jsjzfw, jzfw);
            jzfw.setJzfwfsdxxdz(jsjzfw.getFullAddress());
            xxApiFpkjParam.setTdywJzfw(jzfw);
        }

        //处理不动产销售服务
        if (jsbdcxsfw != null) {
            Bdcxsfw bdcxsfw = new Bdcxsfw();
            BeanUtils.copyProperties(jsbdcxsfw, bdcxsfw);
            bdcxsfw.setWqhtbah(jsbdcxsfw.getWqhtbabh());
            bdcxsfw.setBdcdzxxdz(jsbdcxsfw.getFulladdress());
            xxApiFpkjParam.setTdywBdcxs(bdcxsfw);
        }

        //处理不动产租赁服务
        if (jsbdcjyzlfw != null) {
            Bdcjyzlfw bdcjyzlfw = new Bdcjyzlfw();
            BeanUtils.copyProperties(jsbdcjyzlfw, bdcjyzlfw);
            bdcjyzlfw.setBdcdzxxdz(jsbdcjyzlfw.getFulladdress());
            String zlqqz = jsbdcjyzlfw.getZlqqz();
            if (StringUtils.isNotBlank(zlqqz)) {
                String[] zlrqqstr = zlqqz.split(" ");
                try {
                    bdcjyzlfw.setZlrqq(DateUtils.parseDate(zlrqqstr[0], DateUtils.YYYY_MM_DD));
                    bdcjyzlfw.setZlrqz(DateUtils.parseDate(zlrqqstr[1], DateUtils.YYYY_MM_DD));
                } catch (Exception e) {
                    log.error("不动产租赁服务起止日期格式有误！");
                }
            }
        }

        //处理旅客运输服务
        if (jslkysfwmx != null && !jslkysfwmx.isEmpty()) {
            List<Lkysfwmx> lkysfwmxList = new ArrayList<>();
            for (JsXxLkysfw jsXxLkysfw : jslkysfwmx) {
                Lkysfwmx lkysfwmx = new Lkysfwmx();
                BeanUtils.copyProperties(jsXxLkysfw, lkysfwmx);
                lkysfwmx.setCxrq(jsXxLkysfw.getChuxrq());
                lkysfwmx.setSfzjlx(jsXxLkysfw.getCxrzjlx());
                lkysfwmxList.add(lkysfwmx);
            }
            xxApiFpkjParam.setTdywLkysfwList(lkysfwmxList);
        }

        //处理差额凭证
        if (jscepzmxList != null && !jscepzmxList.isEmpty()) {
            List<Cezs> cezsList = new ArrayList<>();
            for (int i = 0; i < jscepzmxList.size(); i++) {
                JsXxCepz jsXxCepz = jscepzmxList.get(i);
                Cezs cezs = new Cezs();
                BeanUtils.copyProperties(jsXxCepz, cezs);
                if (StringUtils.isNotBlank(jsXxCepz.getQdpfphm())) {
                    cezs.setFphm(jsXxCepz.getQdpfphm());
                }
                cezs.setPzhjje(jsXxCepz.getHjje());
                cezs.setPzly(jsXxCepz.getLy());
                cezsList.add(cezs);
            }
            xxApiFpkjParam.setCezsList(cezsList);
        }

        XxApiFpkjMainParam xxApiFpkjMainParam = new XxApiFpkjMainParam();
        xxApiFpkjMainParam.setAsync("1");
        //河北交行异步回调地址
        xxApiFpkjMainParam.setCallBackUrl("http://localhost:443/taxshare/convert/response/qdpsqCallback");
        xxApiFpkjMainParam.setFpkj(xxApiFpkjParam);

        return Result.OK("开票数据转换成功", JSONObject.toJSONString(xxApiFpkjMainParam));
    }

    /**
     * 数电认证到期时间数据转换
     */
    public static Result<JSONObject> sdrzdqsjcxParamConvert(JSONObject convertData) {
        String error = null;
        JSONObject tax = new JSONObject();
        try {
            String contentBase64 = convertData.getString("content");
            String content = Base64Util.decode(contentBase64);
            JSONObject contentJson = JSONObject.parseObject(content);
            tax.put("kpzddm", contentJson.containsKey("kpzddm") ? contentJson.getString("kpzddm") : "");
        } catch (Exception e) {
            error += "参数转换异常：参数格式传递有误";
        }
        if (StringUtils.isNotEmpty(error)) {
            return Result.error(error);
        } else {
            return Result.OK(tax);
        }
    }

    /**
     * 纸票待开号码查询数据转换
     */
    public static Result<JSONObject> zpfphmcxParamConvert(JSONObject convertData) {
        String error = null;
        JSONObject tax = new JSONObject();
        try {
            String contentBase64 = convertData.getString("content");
            String content = Base64Util.decode(contentBase64);
            JSONObject contentJson = JSONObject.parseObject(content);
            tax.put("kpzddm", contentJson.containsKey("kpzddm") ? contentJson.getString("kpzddm") : "");
            tax.put("fplxdm", contentJson.containsKey("zppzdm") ? contentJson.getString("zppzdm") : "");
        } catch (Exception e) {
            error += "参数转换异常：参数格式传递有误";
        }
        if (StringUtils.isNotEmpty(error)) {
            return Result.error(error);
        } else {
            return Result.OK(tax);
        }
    }


    /**
     * 税控发票开具转换
     * 同步
     *
     * @param convertData
     * @return
     */
    public static Result skFpkjParamConvert(JSONObject convertData) {
        JsXxSkfpkj skfpkj = convertData.toJavaObject(JsXxSkfpkj.class);
        List<JsXxSkfpkjmx> jsmxxx = skfpkj.getMxxx();
        CsXxKpzd csXxKpzd = convert.iDjts.queryKpzdBykpzddm(skfpkj.getKpzddm());
        XxApiFpkjParam xxApiFpkjParam = new XxApiFpkjParam();
        BeanUtils.copyProperties(skfpkj, xxApiFpkjParam);
        xxApiFpkjParam.setFpsqlsh(skfpkj.getDjbh());
        xxApiFpkjParam.setClfs(skfpkj.getClfs());
        xxApiFpkjParam.setSjlx("jkkp");
        xxApiFpkjParam.setSjly("sglr");
        xxApiFpkjParam.setZzfpbz("0");
        xxApiFpkjParam.setHjje(skfpkj.getBhsje());
        xxApiFpkjParam.setHjse(skfpkj.getHsje().subtract(skfpkj.getBhsje()));
        if (StringUtils.isNotBlank(skfpkj.getKhdzdh()) && skfpkj.getKhdzdh().contains(",")) {
            String[] khdzdh = skfpkj.getKhdzdh().split(",");
            xxApiFpkjParam.setKhdz(khdzdh[0]);
            xxApiFpkjParam.setKhdh(khdzdh[1]);
        }
        if (StringUtils.isNotBlank(skfpkj.getKhyhzh()) && skfpkj.getKhyhzh().contains(",")) {
            String[] khyhzh = skfpkj.getKhyhzh().split(",");
            xxApiFpkjParam.setKhyh(khyhzh[0]);
            xxApiFpkjParam.setKhzh(khyhzh[1]);
        }
        String khlxfs = skfpkj.getKhlxfs();
        if(StringUtils.isNotBlank(khlxfs) && khlxfs.contains("@")){
            xxApiFpkjParam.setKhlxyx(khlxfs);
            xxApiFpkjParam.setKhlxfs(null);
        }else if(StringUtils.isNotBlank(khlxfs)){
            xxApiFpkjParam.setKhlxfs(khlxfs);
            xxApiFpkjParam.setKhlxyx(null);
        }
        xxApiFpkjParam.setXfmc(csXxKpzd.getNsrmc());
        xxApiFpkjParam.setXfsh(csXxKpzd.getNsrsbh());
        //处理销方地址和电话
        if (StringUtils.isNotBlank(skfpkj.getXfdzdh()) && skfpkj.getXfdzdh().contains(",")) {
            String[] xfdzdh = skfpkj.getXfdzdh().split(",");
            xxApiFpkjParam.setXfdz(xfdzdh[0]);
            xxApiFpkjParam.setXfdh(xfdzdh[1]);
        }
        if (StringUtils.isBlank(xxApiFpkjParam.getXfdz())) {
            xxApiFpkjParam.setXfdz(csXxKpzd.getXfdz());
        }
        if (StringUtils.isBlank(xxApiFpkjParam.getXfdh())) {
            xxApiFpkjParam.setXfdh(csXxKpzd.getXfdh());
        }
        //处理销方银行和银行账号
        if (StringUtils.isNotBlank(skfpkj.getXfyhzh()) && skfpkj.getXfyhzh().contains(",")) {
            String[] xfyhzh = skfpkj.getXfyhzh().split(",");
            xxApiFpkjParam.setXfyh(xfyhzh[0]);
            xxApiFpkjParam.setXfzh(xfyhzh[1]);
        }
        if (StringUtils.isBlank(xxApiFpkjParam.getXfyh())) {
            xxApiFpkjParam.setXfyh(csXxKpzd.getXfyh());
        }
        if (StringUtils.isBlank(xxApiFpkjParam.getXfzh())) {
            xxApiFpkjParam.setXfzh(csXxKpzd.getXfyhzh());
        }
        //处理开票人 复核人 收款人
        if (StringUtils.isBlank(xxApiFpkjParam.getKpr())) {
            xxApiFpkjParam.setKpr(csXxKpzd.getKpr());
        }
        if (StringUtils.isBlank(xxApiFpkjParam.getFhr())) {
            xxApiFpkjParam.setFhr(csXxKpzd.getFhr());
        }
        if (StringUtils.isBlank(xxApiFpkjParam.getSkr())) {
            xxApiFpkjParam.setSkr(csXxKpzd.getSkr());
        }
        if (jsmxxx.size() > 8) {
            xxApiFpkjParam.setQdbz("1");
        } else {
            xxApiFpkjParam.setQdbz("0");
        }
        xxApiFpkjParam.setHzxxbbh(skfpkj.getTzdbh());
        xxApiFpkjParam.setHcyy(skfpkj.getChyydm());

        //处理明细
        List<XxApiFpkjMxParam> xxApiFpkjMxParamList = new ArrayList<>();
        for (JsXxSkfpkjmx jsXxFpkjMx : jsmxxx) {
            XxApiFpkjMxParam xxApiFpkjMxParam = new XxApiFpkjMxParam();
            BeanUtils.copyProperties(jsXxFpkjMx, xxApiFpkjMxParam);
            xxApiFpkjMxParam.setMxhh(Integer.valueOf(jsXxFpkjMx.getDjhh()));
            String spbm = jsXxFpkjMx.getSsbm();
            CsPbCode code = convert.pbCodeService.getById(spbm);
            if (code != null) {
                xxApiFpkjMxParam.setSpbm(spbm);
                xxApiFpkjMxParam.setSsmc(code.getShortName());
            }
            HsBhsConvertVo hsBhsConvertVo = new HsBhsConvertVo();
            BeanUtils.copyProperties(xxApiFpkjMxParam, hsBhsConvertVo);
            FPUtil.hsBhsConvert(hsBhsConvertVo);
            xxApiFpkjMxParam.setTaxje(hsBhsConvertVo.getTaxje());
            BeanUtils.copyProperties(hsBhsConvertVo, xxApiFpkjMxParam);
            xxApiFpkjMxParamList.add(xxApiFpkjMxParam);
        }
        xxApiFpkjParam.setFpmxs(xxApiFpkjMxParamList);

        XxApiFpkjMainParam xxApiFpkjMainParam = new XxApiFpkjMainParam();
        xxApiFpkjMainParam.setAsync("0");
        xxApiFpkjMainParam.setFpkj(xxApiFpkjParam);

        return Result.OK("税控开票数据转换成功", xxApiFpkjMainParam);
    }

    public static void main(String[] args) {
//        String rq = "2021-03-10 2022-04-07";
//        TaxRequestParamConvert.djtsParamConvert(null);
//        System.out.println(TaxRequestParamConvert.djtsParamConvert(null));

        String yx = "453989955@qq.com";
        System.out.println(yx.contains("@"));
    }

}
